package com.mopa.renren;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.security.MessageDigest;
import java.util.Set;
import java.util.TreeSet;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
//import com.tencent.tauth.http.TDebug;
public class UploadPicClickListener implements OnClickListener {
	private RenRenActivity mActivity;
	public UploadPicClickListener(RenRenActivity activity) {
		mActivity = activity;
	}
	@Override
	public void onClick(View v) {
//		if (!mActivity.satisfyConditions()) {
//
//			return;
//		}
		Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
		intent.addCategory(Intent.CATEGORY_OPENABLE);
		intent.setType("image/*");
		mActivity.startActivityForResult(intent, RenRenActivity.REQUEST_PICK_PICTURE);
		
	}
	public static void uploadPic(final RenRenActivity activity, Uri uri) {
		activity.showDialog(RenRenActivity.PROGRESS);
		Bundle params = new Bundle();
		byte[] buff = null;
		try {
			InputStream is = activity.getContentResolver().openInputStream(uri);
			ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
			byte[] buffer = new byte[1024];
			int len = 0;
			while( (len = is.read(buffer)) !=-1 ){
				outSteam.write(buffer, 0, len);
			}
			outSteam.close();
			is.close();
			buff = outSteam.toByteArray();
		} catch (IOException e) {
			e.printStackTrace();
		}
		params.putString("method", "photos.upload");
		params.putString("v", "1.0");
		params.putString("access_token", Urldecode("162112%7C6.a664e7af236707b7ce749831a712b147.2592000.1331280000-415540622"));
		params.putString("format", "json");//返回类型
		params.putString("caption", "mopa_中国人民真是强大啊." + System.currentTimeMillis() + ".png");//
		//排序
		StringBuffer sb = new StringBuffer();
		Set<String> keys = new TreeSet<String>(params.keySet());
		for (String key : keys) {
			sb.append(key);
			sb.append("=");
			sb.append(params.getString(key));
		}
		sb.append(RenRenItem.SECRET_KEY);
		params.putString("sig",md5(sb.toString()));
//		params.putByteArray("picture", buff);//图片二进制数组
		String result=HttpUtil.uploadFile(RenRenItem.RESTSERVER_URL, params, "upload", "mpa_s.png", "image/png",buff);
		Log.e("debug", "sb.toString():"+result);
	}
    public static  String md5(String string) {
        if (string == null || string.trim().length() < 1) {
            return null;
        }
        try {
            return getMD5(string.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }
    public static  String Urldecode(String accessToken)
    {
    	return URLDecoder.decode(accessToken);
    }
    
    private static String getMD5(byte[] source) {
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            StringBuffer result = new StringBuffer();
            for (byte b : md5.digest(source)) {
                result.append(Integer.toHexString((b & 0xf0) >>> 4));
                result.append(Integer.toHexString(b & 0x0f));
            }
            return result.toString();
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }
}
